//
//  UUWalletVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 22/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUWalletVC.h"
#import "UUWalletListVC.h"
#import "UUResourceManager.h"
#import "UUUserManager.h"
#import "UUWalletWithdrawVC.h"

@interface UUWalletVC ()

@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;
@property (nonatomic, weak) IBOutlet UILabel *withdrawableLabel;
@property (nonatomic, weak) IBOutlet UIButton *withdrawButton;
@property (nonatomic, weak) IBOutlet UIButton *ruleButton;

@end

@implementation UUWalletVC

+ (BOOL)enableTranslucentNavigationBar {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headerView.backgroundColor = COMMON_NAVIGATIONBAR_COLOR;
    
    self.ruleButton.titleLabel.font = [GetResourceManager() iconFontWithSize:12];
    [self.ruleButton setTitleColor:COMMON_TEXT_LIGHT_COLOR forState:UIControlStateNormal];
    [self.ruleButton setTitle:[NSString stringWithFormat:@"%@ 规则说明", GetResourceManager().helpIconText]
                     forState:UIControlStateNormal];
    [self.ruleButton addTarget:self action:@selector(enterAgreement) forControlEvents:UIControlEventTouchUpInside];

    self.withdrawableLabel.textColor = RGB_COLOR(101, 106, 122);
    
    [self.withdrawButton setBackgroundImage:GetResourceManager().buttonImage1 forState:UIControlStateNormal];
    [self.withdrawButton setBackgroundImage:GetResourceManager().buttonImage1HL forState:UIControlStateHighlighted];
    
    NSMutableArray<DKPagingMenuItem *> *tabPageItems = [NSMutableArray arrayWithCapacity:3];
    
    DKPagingMenuItem *allItem = [[DKPagingMenuItem alloc] initWithTitle:@"全部"
                                                     menuViewController:[UUWalletListVC listWithType:UUWalletRecordTypeNone]];
    [tabPageItems addObject:allItem];
    
    DKPagingMenuItem *incomeItem = [[DKPagingMenuItem alloc] initWithTitle:@"收入"
                                                        menuViewController:[UUWalletListVC listWithType:UUWalletRecordTypeIncome]];
    [tabPageItems addObject:incomeItem];
    
    DKPagingMenuItem *withdrawItem = [[DKPagingMenuItem alloc] initWithTitle:@"支出"
                                                          menuViewController:[UUWalletListVC listWithType:UUWalletRecordTypeWithdraw]];
    [tabPageItems addObject:withdrawItem];
    
    UUPagingMenuController *tabPageVC = [[UUPagingMenuController alloc] initWithItems:tabPageItems];
    [self.view addSubview:tabPageVC.view];
    [self addChildViewController:tabPageVC];
    [tabPageVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateMoney];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)enterAgreement {
    UUHybridWebVC *webVC = [UUHybridWebVC new];
    webVC.URLString = @"http://wap.ubaoche.com/app/supplier/rule/detail";
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)updateMoney {
    [GetUserManager() setNeedsUpdateUserInfo];
    WEAK_SELF();
    [GetUserManager() asyncFetchUserInfoWithCompleteBlock:^(UUUserInfo * _Nullable userInfo, NSError * _Nullable error) {
        CGFloat money = ([userInfo.freezeMoney unsignedIntegerValue] + [userInfo.withdrawMoney unsignedIntegerValue]);
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[@(money) toCurrencyWithoutCurrencySymbol]
                                                                                           attributes:@{
                                                                                                        NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                                                        NSFontAttributeName : [UIFont systemFontOfSize:30]
                                                                                                        }];
        
        [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@"元"
                                                                               attributes:@{
                                                                                            NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                                            NSFontAttributeName : [UIFont systemFontOfSize:12]
                                                                                            }]];;
        
        weakSelf.priceLabel.attributedText = attributedText;
        weakSelf.withdrawableLabel.text = [NSString stringWithFormat:@"可提现金额 %@元", [userInfo.withdrawMoney toCurrency]];
    }];
}

- (IBAction)withdraw:(id)sender {
    [self.navigationController pushViewController:[UUWalletWithdrawVC instanceOfAppropriate] animated:YES];
}

@end
